package sample.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.updateLayoutParams
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.netease.core.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.script.tes.*
import java.io.File
import kotlin.math.max
import kotlin.math.min

class OtherActivity : BaseActivity() {
    private val adapter by lazy { Adapter() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.oa)
        val top = systemWindowInsetsTop
        var offset = top
        val x = phoneDefaultDisplayPoint.x
        val y = phoneDefaultDisplayPoint.y
        if ((x == 1080 && y == 1920) || (x == 720 && y == 1280) ||
            (x == 1920 && y == 1080) || (x == 1280 && y == 720) ||
            (x == 960 && y == 540) || (x == 540 && y == 960)) {
            offset = 0
        }
        findViewById<View>(R.id.root).setPadding(offset, 0, 0, 0)
        findViewById<View>(R.id.fitsSys).setPadding(top / 2, top / 2, top / 2, top / 2)
        findViewById<View>(R.id.back).setOnClickCallback { finish() }
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.itemAnimator = null
        recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
        recyclerView.adapter = adapter
        lifecycleScope.launch {
            val dataList = CrashUtils.getScreenshotListByDisk()
            adapter.set(dataList)
        }
        findViewById<View>(R.id.menuDelete).setOnClickCallback {
            CrashUtils.deleteAllScreenshot()
            adapter.set(listOf())
        }
    }

    class Adapter: RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        private val dataList: MutableList<File> = mutableListOf()
        fun set(sourceList: List<File>) {
            dataList.clear()
            dataList.addAll(sourceList)
            notifyDataSetChanged()
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return ViewHolder(parent)
        }

        override fun getItemCount(): Int  = dataList.size

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder as? ViewHolder?)?.bind(dataList.getOrNull(position))
        }
    }

    class ViewHolder(
        parent: ViewGroup,
        view: View = LayoutInflater.from(parent.context).inflate(R.layout.cil, parent, false),
    ) : RecyclerView.ViewHolder(view) {
        companion object {
            private var obh: Int? = null
            private var obw: Int? = null
            val bw get() = obw ?: readByDisk("obw")?.toIntOrNull()?.also { obw = it }
            val bh get() = obh ?: readByDisk("obh")?.toIntOrNull()?.also { obh = it }
            fun saveBw(value: Int) {
                writeToDisk("obw", value.toString())
                obw = value
            }
            fun saveBh(value: Int) {
                writeToDisk("obh", value.toString())
                obh = value
            }
        }

        fun bind(data: File?) {
            if (data == null) return
            itemView.tag = data
            val preBw = bw
            val preBh = bh
            if (preBw != null && preBh != null) {
                itemView.findViewById<View>(R.id.errorImage).updateLayoutParams<ConstraintLayout.LayoutParams> {
                    val w = max(preBw, preBh)
                    val h = min(preBw, preBh)
                    dimensionRatio= "H,${w}:${h}"
                }
            }
            if (data.exists()) {
                appScopeLaunch(Dispatchers.Main) {
                    val bitmap = FileUtils.readBitmapFromDisk(data)
                    if (bitmap != null) {
                        val bitmapW = bw ?: bitmap.width.also { saveBw(it) }
                        val bitmapH = bh ?: bitmap.height.also { saveBh(it) }
                        itemView.findViewById<View>(R.id.errorImage).updateLayoutParams<ConstraintLayout.LayoutParams> {
                            val w = max(bitmapW, bitmapH)
                            val h = min(bitmapW, bitmapH)
                            dimensionRatio= "H,${w}:${h}"
                        }
                        itemView.findViewById<ImageView>(R.id.errorImage).setImageBitmap(bitmap)
                    }
                    itemView.findViewById<TextView>(R.id.errorTime).text = data.lastModified().toTime("yyyy/MM/dd HH:mm:ss")
                    itemView.findViewById<View>(R.id.errorTip).visibility = View.INVISIBLE
                }
            }
        }
    }
}

